Test distance_transform_edt() Performance

scipy.ndimage.morphology.distance_transform_edt


In [1]:
%pylab inline


Populating the interactive namespace from numpy and matplotlib

In [2]:
from IPython.display import IFrame
IFrame('http://docs.scipy.org/doc/scipy-0.14.0/reference/generated/scipy.ndimage.morphology.distance_transform_edt.html',
    width='100%', height=400)


Out[2]:

In [3]:
import numpy as np
from scipy.ndimage.morphology import distance_transform_edt

In [4]:
a = np.array(([0,1,1,1,1],
                  [0,0,1,1,1],
                  [0,1,1,1,1],
                  [0,1,1,1,0],
                  [0,1,1,0,0]))
dist, ind = distance_transform_edt(a, sampling=[2, 0.11], return_indices=True)
dist


Out[4]:
array([[ 0.  ,  0.11,  0.22,  0.33,  0.44],
       [ 0.  ,  0.  ,  0.11,  0.22,  0.33],
       [ 0.  ,  0.11,  0.22,  0.33,  0.44],
       [ 0.  ,  0.11,  0.22,  0.11,  0.  ],
       [ 0.  ,  0.11,  0.11,  0.  ,  0.  ]])

In [5]:
ind


Out[5]:
array([[[0, 0, 0, 0, 0],
        [1, 1, 1, 1, 1],
        [2, 2, 2, 2, 2],
        [3, 3, 3, 3, 3],
        [4, 4, 4, 4, 4]],

       [[0, 0, 0, 0, 0],
        [0, 1, 1, 1, 1],
        [0, 0, 0, 0, 0],
        [0, 0, 0, 4, 4],
        [0, 0, 3, 3, 4]]])

In [6]:
n = 200
a = np.random.randint(low=0, high=2, size=(n,n,n))
%timeit dist = distance_transform_edt(a, sampling=[2.001, 0.11, 0.73])


1 loop, best of 3: 1.67 s per loop

In [7]:
a = np.random.rand(n,n,n)
a[a < 0.5] = 0
a[a >= 0.5] = 1
%timeit dist = distance_transform_edt(a, sampling=[2.001, 0.11, 0.73])


1 loop, best of 3: 1.78 s per loop

In [8]:
a = np.random.rand(n,n,n)
a[a < 0.5] = 0
a[a >= 0.5] = 1
%timeit dist, ind = distance_transform_edt(a, sampling=[2.001, 0.11, 0.73], return_indices=True)


1 loop, best of 3: 1.71 s per loop